package com.cyn.chat.controller;

import com.cyn.chat.service.impl.GptServiceImpl;
import com.cyn.chat.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class ChatGptController {
    private final GptServiceImpl gptService;

    @Autowired
    public ChatGptController(GptServiceImpl gptService) {
        this.gptService = gptService;
    }
    private List<String> context = new ArrayList<>(); // 上下文变量



    @GetMapping({"/chat","/"} )
    public ModelAndView chat(){
        ModelAndView mv = new ModelAndView("chat");
        return mv;
    }
    @PostMapping("/chat")
    public R chatPost(@RequestBody Map<String, String> request){
        String message = request.get("message");
        // 传递上下文并获取回答,两个/n
        String response = gptService.chat(message);

        // 更新上下文
        context.add(message);
        context.add(response);

        // 返回本次回答的内容
        return R.ok(response);
    }
}